package chatGUI;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JTextField;
import javax.swing.JLabel;


/**
 * <b>Project ChatSystemG2 - INSA 4INFOA1 - 2013/2014</b>
 * <P>
 * <i>Package  chatGUI</i> <br>
 * WindowConexion is the window displayed when a user opens the chatsystem, or is disconnected.
 * <br>
 * It implements the interfaces ActionListeners and WindowListener, and extends the class JFrame.
 * 
 * @author Sergio Gonzalez - Marie-Charlotte Bideaud
 */

public class WindowConnexion extends JFrame implements ActionListener, WindowListener {	
	
	private static final long serialVersionUID = 1L;	
	private ChatGUI cgui;
	private JTextField textField;
	private JLabel lblUsername;
	private JLabel lblAdress;
	private JPanel panel;
	private JButton btnConnection;
	
	/**
	 * Constructor for the class WindowConnexion
	 * 
	 * @param cgui : ChatGUI
	 * @throws UnknownHostException 
	 */
	public WindowConnexion(ChatGUI cgui) throws UnknownHostException {
		this.cgui = cgui;
		this.initComponents();
	}
	
	/**
	 * Method called when the window is created
	 * @throws UnknownHostException 
	 */
	private void initComponents() throws UnknownHostException {
		// Definition of the layouts
		this.setTitle("Connection");
		this.setLocation(550, 150);
		this.setResizable(false);
		this.addWindowListener(this);
		this.setSize(300, 150);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		panel = new JPanel();
		this.add(panel);
		panel.setLayout(null);
		
		lblUsername = new JLabel("Username:");
		lblUsername.setBounds(10, 20, 80, 25);
		panel.add(lblUsername);
		
		lblAdress = new JLabel(""+InetAddress.getLocalHost());
		lblAdress.setBounds(10, 95, 140, 25);
		panel.add(lblAdress);
		
		textField = new JTextField();
		textField.setBounds(100, 20, 160, 25);
		textField.setColumns(10);
		
		btnConnection = new JButton("Connection");
		btnConnection.setBounds(100, 70, 100, 25);
		btnConnection.addActionListener(this);
		btnConnection.setActionCommand("Connect");
		
		panel.add(lblUsername);
		panel.add(textField);
		panel.add(btnConnection);
		
		this.getRootPane().setDefaultButton(btnConnection);
	}

	/**
	 * Method called when the user presses the connect button
	 * 
	 * @param e : ActionEvent
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Connect")) {
			cgui.setUsername(textField.getText());
		}
	}

	/**
	 * Method to display the window : when the user gets connected
	 */
	public void display() {
		this.setVisible(true);
	}

	/**
	 * Method to close the window : when the user gets disconnected
	 */
	public void close() {
		this.dispose();
	}

	/**
	 * Method called when the user wants to close the windowConnexion 
	 * 
	 * @param e : WindowEvent
	 */
	public void windowClosing(WindowEvent e) {	
		// To confirm the user wants to quit the chat 
		int option = JOptionPane.showConfirmDialog(null, "Are you sure to want to quit ?", "Quit", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
		
		if(option == JOptionPane.YES_OPTION)
		{
			System.exit(0);
		}
	}

	// Methods of the interfaces
	public void windowDeactivated(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowOpened(WindowEvent e) {}
	public void windowActivated(WindowEvent e) {}
	public void windowClosed(WindowEvent e) {}
}
